{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.极限与连续（高数上27页）\n",
    "定义 设函数y=f(x)在点x,的某一邻域内有定义,如果\n",
    "lim Δy= lim [f(xo+Δx)-f(x,)]=0, \n",
    "那么就称函数y=f(x)在点x,连续.\n",
    "\n",
    "设函数f(x)在点x0的某一去心邻域内有定义.如果存在常数A,对于任意给定的正数ε（不论它多么小），总存在正数δ,使得当x满足不等式0<|x-x0|<δ时,对应的函数值f(x)都满足不等式\n",
    "If(x)-AI<ε\n",
    "那么常数A就叫做函数f(x)当x→x时的极限，记作\n",
    "limf(x)=A 或f(x)→A （当x→x0).\n",
    "\n",
    "那么如何使用python语言解决这类高等数学问题呢？我们通过Function构造函数，可以创建一个未知函数符号。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "f=Functions(‘f’)\n",
    "```\n",
    "可以通过向symbols函数中传入cls参数来创建未知函数符号。\n",
    "\n",
    "```python\n",
    "f = symbols('f', cls=Function)\n",
    "```\n",
    "例9.x 求极限 $$ \\lim \\limits_{x \\to 0} \\frac{e^x-1}{x}=1 $$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "\n",
    "# 定义表达式\n",
    "expr = (sp.exp(x) - 1) / x\n",
    "\n",
    "# 求极限\n",
    "limit_result = sp.limit(expr, x, 0)\n",
    "print(limit_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.积分（高数上184页）\n",
    "\n",
    "定义1 如果在区间I上，可导函数F(x)的导函数为f(x)，即对任一\n",
    "xeI, 都有\n",
    "F'(x)=f(x)或 dF(x)=f(x)dx, \n",
    "那么函数F(x)就称为f(×)（或f(x)dx)在区间上的一个原函数\n",
    "例如，因(sin X)'=cos x,故 sinx是cos.x的一个原函数。\n",
    "\n",
    "定义2 在区间I上，函数f(x)的带有任意常数项的原函数称为f(x) （或\n",
    "f(x)dx)在区间I上的不定积分，记作\n",
    "∫f(x)dx.\n",
    "其中记号∫称为积分号,f(x)称为被积函数，f(x)dx称为被积表达式，x称为积分变量。\n",
    "\n",
    "使用python语言解决积分问题：\n",
    "使用Integrate()函数可以轻松完成各种积分的运算，Integrate()函数的使用方法如下：\n",
    "\n",
    "Integrate(expressions, (integration_variable[, lower_limit, upper_limit])\n",
    "\n",
    "其中第一个参数为待积分的表达式，第二个参数是以元组形式传入积分变量以及其上下界。\n",
    "## 2.1.不定积分\n",
    "\n",
    "当Integrate()函数第二个参数不传入积分上下界时，默认计算表达式的不定积分：\n",
    "\n",
    "例：求不定积分 $$\\int \\frac{\\arctan{\\sqrt{x}}}{(1+x)\\sqrt{x}}$$。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "atan(sqrt(x))**2\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "# 定义被积函数\n",
    "expr = sp.atan(sp.sqrt(x)) / ((1 + x) * sp.sqrt(x))\n",
    "# 计算不定积分\n",
    "integral_result = sp.integrate(expr, x)\n",
    "print(integral_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2定积分\n",
    "\n",
    "当Integrate()函数第二个参数传入积分上下界时，默认计算表达式在给定范围内的定积分：\n",
    "\n",
    "例：求定积分$$\\int_{0}^{log 2} e^{-x} dx$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sqrt(pi)*erf(log(2))/2\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "# 定义被积函数\n",
    "expr = sp.exp(-x**2)\n",
    "# 计算定积分\n",
    "integral_result = sp.integrate(expr, (x, 0, sp.log(2)))\n",
    "print(integral_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3广义积分\n",
    "\n",
    "求广义积分的方法与求定积分的方法是相同而，不同的是广义积分的计算结果有可能是无穷——$$\\infty$$。\n",
    "\n",
    "## 2.4无穷积分\n",
    "\n",
    "例：计算积分$$\\int_{0}^{+\\infty} e^{-x^2} dx$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sqrt(pi)/2\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "# 定义被积函数\n",
    "expr = sp.exp(-x**2)\n",
    "# 计算无穷积分\n",
    "integral_result = sp.integrate(expr, (x, 0, sp.oo))\n",
    "print(integral_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.5瑕积分\n",
    "\n",
    "例：判断广义积分$$\\int_0^2 \\frac{1}{(1-x)^2}$$的敛散性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The integral converges and the result is: oo\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "# 定义被积函数\n",
    "expr = 1 / (1 - x) ** 2\n",
    "# 尝试计算广义积分\n",
    "try:\n",
    "    integral_result = sp.integrate(expr, (x, 0, 2))\n",
    "    print(\"The integral converges and the result is:\", integral_result)\n",
    "except sp.IntegralError as e:\n",
    "    print(\"The integral diverges. Error message:\", str(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.级数（高数下251页）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "人们认识事物在数量方面的特性，往往有一个由近似到精确的过程.在这种\n",
    "认识过程中，会遇到由有限个数量相加到无穷多个数量相加的问题.\n",
    "例如计算半径为R的圆面积A，具体做法如下：作圆的内接正六边形，算出\n",
    "这六边形的面积a，它是圆面积A的一个粗糙的近似值.为了比较准确地计算\n",
    "出A的值，我们以这个正六边形的每一边为底分别作一个顶点在圆周上的等腰\n",
    "三角形，算出这六个等腰三角形的面积之和a2，.那么a1+a2，（即内接\n",
    "正十二边形的面积）就是A的一个较好的近似值.同样地，在这正十二边形的每\n",
    "一边上分别作一个顶点在圆周上的等腰三角形，算出这十二个等腰三角形的面\n",
    "积之和a3.那么a1+a2+a3（即内接正二十四边形的面积）是A的一个更好的近\n",
    "似值.如此继续下去，内接正3×2ⁱ边形的面积就逐步逼近圆面积；\n",
    "A≈a1， A≈a1+a2, A≈a1+a2+a3，…, \n",
    "A≈a1+a2+...+an.\n",
    "如果内接正多边形的边数无限增多，即n无限增大，那么和a1+a2+…+an的极限就是所要求的圆面积A.这时和式中\n",
    "的项数无限增多，于是出现了无穷多个数量依次相加的数学式子.\n",
    "\n",
    "那么我们试着利用python语言解决这种问题，需要利用Sum函数。\n",
    "\n",
    "使用symPy.Sum对级数进行求解，十分简单。\n",
    "\n",
    "Sum()函数的使用方法如下：\n",
    "\n",
    "```python\n",
    "Sum(func, (variables, start, end))\n",
    "```\n",
    "\n",
    "其中第一个参数为待求级数通项，第二个元组形式的参数接受数变量以及级数的起始与终止数值。\n",
    "\n",
    "另外需要说明的是，使用Sum求和，并不会会自动对极限、积分、求和以及乘积进行计算，它的作用仅仅是创建一个和式，可以使用Sum.doit()来完成这种操作（如果可以的话）。如果希望计算数值，可以使用Sum.evalf()\n",
    "\n",
    "## 3.1级数判敛\n",
    "例：判断级数$$\\sum_{n=1}^{+\\infty} \\frac{n^2}{2^n}$$的收敛性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The series converges.\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量n\n",
    "n = sp.Symbol('n')\n",
    "# 定义级数\n",
    "series = n ** 2 / (2 ** n)\n",
    "# 使用sympy的求和函数判断收敛性\n",
    "sum_result = sp.Sum(series, (n, 1, sp.oo))\n",
    "if sum_result.is_convergent:\n",
    "    print(\"The series converges.\")\n",
    "else:\n",
    "    print(\"The series diverges.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2幂级数求和\n",
    "例：求$$S(x) = \\sum_{n=1}^{\\infty} \\frac{n^3}{(n+1)}x^n$$的和函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum function is: Piecewise((x*((-6*x**2 + 4*x - 2)/(x**4 - 3*x**3 + 3*x**2 - x) + 2*log(1 - x)/x**2)/2, Abs(x) < 1), (Sum(n**3*x**n/(n + 1), (n, 1, oo)), True))\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x和n\n",
    "x = sp.Symbol('x')\n",
    "n = sp.Symbol('n')\n",
    "# 定义幂级数的通项\n",
    "a_n = (n ** 3 / (n + 1)) * x ** n\n",
    "# 使用sympy的求和函数求幂级数的和\n",
    "S = sp.Sum(a_n, (n, 1, sp.oo))\n",
    "# 尝试计算和函数\n",
    "try:\n",
    "    sum_result = sp.summation(a_n, (n, 1, sp.oo))\n",
    "    print(\"The sum function is:\", sum_result)\n",
    "except Exception as e:\n",
    "    print(\"Failed to find the sum function. Error:\", str(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.3幂级数展开\n",
    "\n",
    "使用series()函数可以将一个函数展开为幂级数。\n",
    "\n",
    "series()函数的使用方法如下：\n",
    "\n",
    "```python\n",
    "series(func, x, x0, n)\n",
    "```\n",
    "\n",
    "其中func为待展开函数，\n",
    "x为目标展开变量，如果不设置该参数，程序将自动判别，x0为站开点，n为展开阶数，默认n=6，即展开到为止。\n",
    "\n",
    "例：将函数$$f(x) = \\frac{x}{9+x^2}$$展开成$$x$$的幂级数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x/9 - x**3/81 + x**5/729 + O(x**6)\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "# 定义函数\n",
    "f = x / (9 + x**2)\n",
    "# 将函数展开成幂级数\n",
    "n = 6  # 展开阶数\n",
    "series = sp.series(f, x, 0, n)\n",
    "print(series)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用fourier_series()函数可以将一个函数展开为傅里叶级数。\n",
    "\n",
    "fourier_series()函数的使用方法如下：\n",
    "\n",
    "```python\n",
    "fourier_series(func, (var, start ,end))\n",
    "```\n",
    "\n",
    "其中func为待展开函数，\n",
    "var为目标展开变量，start为展开区间的左侧，end为展开区间的右侧。\n",
    "\n",
    "例：设的周期为1，它在[0,1]的表达式为$$f(t) = t (0\\le t \\lt 1)$$，试将$$f(t)$$展开成傅立叶级数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FourierSeries(t, (t, 0, 1), (1/2, SeqFormula(2*Piecewise((sin(2*_n*pi)/(2*_n*pi) + cos(2*_n*pi)/(4*_n**2*pi**2) - 1/(4*_n**2*pi**2), (_n > -oo) & (_n < oo) & Ne(_n, 0)), (1/2, True))*cos(2*_n*pi*t), (_n, 1, oo)), SeqFormula(2*Piecewise((-cos(2*_n*pi)/(2*_n*pi) + sin(2*_n*pi)/(4*_n**2*pi**2), (_n > -oo) & (_n < oo) & Ne(_n, 0)), (0, True))*sin(2*_n*pi*t), (_n, 1, oo))))\n"
     ]
    }
   ],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量t\n",
    "t = sp.Symbol('t')\n",
    "\n",
    "# 定义函数f(t)\n",
    "f_t = t\n",
    "\n",
    "# 使用fourier_series函数展开\n",
    "from sympy.series.fourier import fourier_series\n",
    "series = fourier_series(f_t, (t, 0, 1))\n",
    "\n",
    "# 打印傅里叶级数\n",
    "print(series)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.4幂级数计算\n",
    "\n",
    "例：使用麦克劳林展开式求前5， 10， 20项数值，近似计算\n",
    "\n",
    "```python\n",
    "[]:f=Function('f')\n",
    "\n",
    "f5=Lambda(x, series(exp(x), x, 0, 5)).subs(O(x\\*\\*5), 0)\n",
    "\n",
    "f10=Lambda(x, series(exp(x), x,0, 10)).subs(O(x\\*\\*10), 0)\n",
    "\n",
    "f20=Lambda(x, series(exp(x), x,0, 20)).subs(O(x\\*\\*20), 0)\n",
    "\n",
    "float(f5(2)), float(f10(2)), float(f20(2))\n",
    "\n",
    "[]:(7.0, 7.388712522045855, 7.389056098930174)\n",
    "```\n",
    "在上例中为了输出美观起见，使用0代替了高阶无穷小。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
